# 导包
import unittest
import app
from api.login import LoginAPI

# 定义测试类
class TestLoginAPI(unittest.TestCase):
    # 前置处理
    def setUp(self):
        # 实例化
        self.login_api = LoginAPI()

    # 登陆成功
    def test001(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "13800000002", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200,response.status_code)
        self.assertEqual(True,response.json().get('success'))
        self.assertEqual(10000,response.json().get('code'))
        self.assertIn('操作成功',response.json().get('message'))

    # 提取token信息,传给app.headers_data['Authorization']
        app.TOKEN ="Bearer " + response.json().get("data")
        print(app.TOKEN)
        app.headers_data['Authorization'] = app.TOKEN
        print(app.headers_data['Authorization'])


    # 不输入手机号
    def test002(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200,response.status_code)
        self.assertEqual(False,response.json().get('success'))
        self.assertEqual(20001,response.json().get('code'))
        self.assertIn('用户名或密码错误',response.json().get('message'))
        # self.assertEqual(None,response.json().get('data'))

    # 不输入密码
    def test003(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "13800000002", "password": ""})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    # 手机号长度小于11位
    def test004(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "1380000000", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    # 手机号长度大于11位
    def test005(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "138000000023", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    def test006(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "error", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    def test007(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "13800000112", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    def test008(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "13800000002", "password": "123456","parameter":"hahaha"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get('success'))
        self.assertEqual(10000, response.json().get('code'))
        self.assertIn('操作成功', response.json().get('message'))

    def test009(self):
        # 调用登陆接口
        response = self.login_api.login({ "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    def test010(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "13800000002"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    def test011(self):
        # 调用登陆接口
        response = self.login_api.login(None)
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(99999, response.json().get('code'))
        self.assertIn('抱歉，系统繁忙，请稍后重试', response.json().get('message'))

    def test012(self):
        # 调用登陆接口
        response = self.login_api.login({"mobiel": "13800000002", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))

    def test013(self):
        # 调用登陆接口
        response = self.login_api.login({"mobile": "13800000002", "passwd": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get('success'))
        self.assertEqual(20001, response.json().get('code'))
        self.assertIn('用户名或密码错误', response.json().get('message'))